<template>

  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img
          src="../assets/heima.png"
          alt=""
        >
        <span>电商后台管理系统</span>
      </div>
      <el-button
        type="info"
        @click="home_tc"
      >退出</el-button>
    </el-header>
    <!-- 内容区域 -->
    <el-container>
      <!-- 左侧导航 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
            <!-- 展示隐藏左侧菜单 -->
          <div class="qhcd" @click="zsyc">|||</div>
        <!-- 左侧菜单 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="pink"
          unique-opened
          :collapse='isCollapse'
          :collapse-transition = 'false'
          router
          :default-active="activePath"
        >
            <!-- 一级菜单 -->
           <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id" >
            <template slot="title">
                <!-- 一级图标 -->
              <i :class="tz[item.id]"></i>
              &nbsp;
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+SonItem.path" v-for="SonItem in item.children" :key="SonItem.id">
              <template slot="title">
                <!-- 二级图标 -->
                <i class="el-icon-menu"></i>
                <span>{{SonItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>


        </el-menu>
      </el-aside>
      <el-main>
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
    data() {
        return {
            menulist:[],
            tz:{
                125:'iconfont icon-user',
                103:'iconfont icon-tijikongjian',
                101:'iconfont icon-shangpin',
                102:'iconfont icon-danju',
                145:'iconfont icon-baobiao'
            },
            isCollapse:false,
            activePath:""
        }
    },
    created(){
      this.getMenuList()
      // 解决页面刷新时二级菜单不选中的问题
      this.activePath = this.$route.path    
    },
    methods: {
    home_tc() {
      window.sessionStorage.removeItem("token");
      var tokenStr = window.sessionStorage.getItem("token");
      if (!tokenStr) return this.$router.push("/login");
    },
    async getMenuList(){
        var {data:res} = await this.$http.get('menus')
        if(res.meta.status != 200) return this.$message.error(res.meta.msg)
        this.menulist = res.data
        //  console.log(this.menulist);
    },
    // 侧边栏的折叠
    zsyc(){
        this.isCollapse = !this.isCollapse
    }
  }
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #333744;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
      border-right: none;
     background-color: #333744;
  }
  
}
.el-main {
  background-color: #eaedf1;
}
.qhcd{
      background-color: #4a5064;
      font-size: 10px;
      line-height: 24px;
      color: #fff;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
  }
</style>